package com.yksj.healthtalk.ui.server;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog.OnClickSureBtnListener;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;
import com.yksj.healthtalk.utils.WheelUtils;
/**
 * 举报
 * @author zhao
 */
public class ServerCenterReportFragment extends BaseFragmentActivity implements OnClickListener {
	EditText mEditText;
	TextView mTextView;
	String customerId;//客户id
	String merchantId;//商户id
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.server_center_report_layout);
		initTitle();
		initView();
	}
	
	
	private void initView() {
		customerId = SmartFoxClient.getLoginUserId();
		merchantId = getIntent().getStringExtra("id");
		mEditText  = (EditText)findViewById(R.id.commentEdit);
		mTextView = (TextView)findViewById(R.id.number_txt);
		findViewById(R.id.submit).setOnClickListener(this);
		mEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
				mTextView.setText(s.length()+"/140");
			}
		});
		titleTextV.setText(getIntent().getStringExtra("title"));
		titleLeftBtn.setOnClickListener(this);
		mTextView.setTextColor(Color.rgb(29, 29, 29));
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setText("确定");
	}


	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_right2:
			submitFeedback();
			break;
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	
	/**
	 * 提交意见内容
	 */
	private void submitFeedback(){
		WheelUtils.hideInput(ServerCenterReportFragment.this,mEditText.getWindowToken());
		String value = mEditText.getEditableText().toString().trim();
		if("".equals(value)){
			SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  getString(R.string.opinion_null));
			return;
		}
		
		if(!SystemUtils.isNetWorkValid(this)){
			ToastUtil.showShort(this, R.string.getway_error_note);
			return;
		}
		HttpRestClient.dohttpFeedBackServerCenter(merchantId,value ,new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(JSONObject response) {
				super.onSuccess(response);
				if(response.containsKey("error_code")){
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), response.getString("error_message"));
				}else{
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), response.getString("info"),new OnClickSureBtnListener() {
						@Override
						public void onClickSureHander() {
							ServerCenterReportFragment.this.finish();
						}
					});
				}
			}
		});
	}
	
}
